package cn.stylefeng.guns.modular.order.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.agv.mapper.HikAgvRoadWayMapper;
import cn.stylefeng.guns.modular.agv.model.params.HikAgvRoadWayParam;
import cn.stylefeng.guns.modular.agv.model.params.XlbClientParamVo;
import cn.stylefeng.guns.modular.agv.model.result.HikAgvRoadWayResult;
import cn.stylefeng.guns.modular.agv.model.result.XlbClientData;
import cn.stylefeng.guns.modular.agv.model.result.XlbClientRS;
import cn.stylefeng.guns.modular.basic.model.params.BasicXlbOauth2tokenParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicXlbOauth2tokenResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicXlbOauth2tokenService;
import cn.stylefeng.guns.modular.order.entity.OrderOutpreparePlatooncar;
import cn.stylefeng.guns.modular.order.mapper.OrderOutpreparePlatooncarMapper;
import cn.stylefeng.guns.modular.order.model.params.OrderOutpreparePlatooncarParam;
import cn.stylefeng.guns.modular.order.model.result.OrderOutpreparePlatooncarResult;
import cn.stylefeng.guns.modular.order.service.OrderOutpreparePlatooncarService;
import cn.stylefeng.guns.modular.transport.utils.XlbApiUtils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出库订单预排车表 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2023-11-08
 */
@Service
public class OrderOutpreparePlatooncarServiceImpl extends ServiceImpl<OrderOutpreparePlatooncarMapper, OrderOutpreparePlatooncar> implements OrderOutpreparePlatooncarService {

    @Resource
    private BasicXlbOauth2tokenService basicXlbOauth2tokenService;

    @Resource
    private HikAgvRoadWayMapper hikAgvRoadWayMapper;


    @Value("${hxl.api.url}")
    private String hxlApiUrl;

    @Override
    public void add(OrderOutpreparePlatooncarParam param) {
        OrderOutpreparePlatooncar entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(OrderOutpreparePlatooncarParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(OrderOutpreparePlatooncarParam param) {
        OrderOutpreparePlatooncar oldEntity = getOldEntity(param);
        OrderOutpreparePlatooncar newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void updateByDianHua(String stat, String sijidianhua) {
        this.baseMapper.updateByDianHua(stat, sijidianhua);
    }

    @Override
    public OrderOutpreparePlatooncarResult findBySpec(OrderOutpreparePlatooncarParam param) {
        return null;
    }

    @Override
    public List<OrderOutpreparePlatooncarResult> findListBySpec(OrderOutpreparePlatooncarParam param, Long operateId) {

        BasicXlbOauth2tokenParam oauth2tokenParam = new BasicXlbOauth2tokenParam();
        oauth2tokenParam.setGsnumber("66666");
        if(operateId == 39L || operateId == 44L || operateId == 86){ //齐河、天津、济宁
            oauth2tokenParam.setGsnumber("66666");
        }else if(operateId == 42L || operateId == 83L){ // 沈阳仓、长春仓
            oauth2tokenParam.setGsnumber("66666");
        }else if(operateId == 89L){ // 吖滴吖滴
            oauth2tokenParam.setGsnumber("66555");
        }else{
            oauth2tokenParam.setGsnumber("1000");
        }

        BasicXlbOauth2tokenResult oauth2tokenResult = basicXlbOauth2tokenService.findBySpec(oauth2tokenParam);
        XlbClientParamVo apiParam = new XlbClientParamVo();
        Map<String, List<HikAgvRoadWayResult>> xlbClientRSMap = new HashMap<>();
        List<HikAgvRoadWayResult> roadWayList = null;
        try {
            Map<String,HikAgvRoadWayResult> roadWayMap = new HashMap<>();
            if(operateId == 39L){
                List<HikAgvRoadWayResult> listAll = hikAgvRoadWayMapper.getListAll();
                for(HikAgvRoadWayResult obj:listAll){
                    roadWayMap.put(obj.getRoadWayCode(),obj);
                }
            }
            XlbClientRS clientInfo = XlbApiUtils.getClientInfo(this.hxlApiUrl, apiParam, oauth2tokenResult.getOauth2TokenXlb());
            if (clientInfo != null && clientInfo.getCode() == 0 && clientInfo.getData() != null && clientInfo.getData().size() > 0) {
                for (XlbClientData obj : clientInfo.getData()) {
                    String channel_name = obj.getChannel_name();
                    if(ToolUtil.isNotEmpty(channel_name)){
                        channel_name = channel_name.substring(0,3);
                        channel_name = StringUtils.stripStart(channel_name, "0");
                        if(roadWayMap.get(channel_name) != null){
                            if(xlbClientRSMap.get(obj.getClient_id()) == null){
                                roadWayList = new ArrayList<>();
                                roadWayList.add(roadWayMap.get(channel_name));
                                xlbClientRSMap.put(obj.getClient_id(),roadWayList);
                            }else{
                                xlbClientRSMap.get(obj.getClient_id()).add(roadWayMap.get(channel_name));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<OrderOutpreparePlatooncarResult> list = this.baseMapper.findListBySpec(param, null);
        for (OrderOutpreparePlatooncarResult obj : list) {
            if(xlbClientRSMap.get(obj.getClientId()) != null){
                obj.setRoadWays(xlbClientRSMap.get(obj.getClientId()));
//                obj.getRoadWays().addAll(xlbClientRSMap.get(obj.getClientId()));
//                obj.setRoadWayCode(xlbClientRSMap.get(obj.getClientId()).getRoadWayCode());
//                obj.setSeq(xlbClientRSMap.get(obj.getClientId()).getSeq());
//                obj.setTrainRoadWaySeq(xlbClientRSMap.get(obj.getClientId()).getTrainRoadWaySeq());
//                obj.setDockPlat(xlbClientRSMap.get(obj.getClientId()).getDockPlat());
            }
        }
        return list;
    }

    @Override
    public LayuiPageInfo findPageBySpec(OrderOutpreparePlatooncarParam param, String condition, Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param, condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(OrderOutpreparePlatooncarParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private OrderOutpreparePlatooncar getOldEntity(OrderOutpreparePlatooncarParam param) {
        return this.getById(getKey(param));
    }

    private OrderOutpreparePlatooncar getEntity(OrderOutpreparePlatooncarParam param) {
        OrderOutpreparePlatooncar entity = new OrderOutpreparePlatooncar();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
